Passed
Branch master (32b4c5)
by Askupa
01:33
created

Mivhak.component(ꞌtab-paneꞌ).methods.setEditor   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 31
rs 8.8571
1
Mivhak.component('tab-pane', {
2
    template: '<div class="mivhak-tab-pane"><div class="mivhak-tab-pane-inner"></div></div>',
3
    props: {
4
        resource:       null,
5
        editor:         null,
6
        index:          null,
7
        padding:        10,
8
        mivhakInstance: null
9
    },
10
    created: function() {
11
        this.setEditor();
12
        this.fetchRemoteSource();
13
        this.markLines();
14
        
15
        this.$el = $(this.resource.pre).wrap(this.$el).parent().parent();
16
        this.$el.find('.mivhak-tab-pane-inner').css({margin: this.mivhakInstance.options.padding});
17
        this.setScrollbars();
18
        
19
    },
20
    methods: {
21
        getTheme: function() {
22
            return this.mivhakInstance.options.theme === 'light' ? 'clouds' : 'ambiance';
23
        },
24
        fetchRemoteSource: function() {
25
            var $this = this;
26
            if(this.resource.source) {
27
                $.ajax(this.resource.source).done(function(res){
28
                    $this.editor.setValue(res,-1);
29
                    
30
                    // Refresh code viewer height
31
                    $this.mivhakInstance.callMethod('setHeight',$this.mivhakInstance.options.height);
32
                    
33
                    // Refresh scrollbars
34
                    raf(function(){
35
                        $this.vscroll.refresh();
36
                        $this.hscroll.refresh();
37
                    });
38
                });
39
                
40
            }
41
        },
42
        setScrollbars: function() {
43
            var $inner = $(this.resource.pre),
44
                $outer = this.$el.find('.mivhak-tab-pane-inner');
45
            
46
            this.vscroll = Mivhak.render('vertical-scrollbar',{editor: this.editor, $inner: $inner, $outer: $outer, mivhakInstance: this.mivhakInstance});
47
            this.hscroll = Mivhak.render('horizontal-scrollbar',{editor: this.editor, $inner: $inner, $outer: $outer, mivhakInstance: this.mivhakInstance});
48
            
49
            this.$el.append(this.vscroll.$el, this.hscroll.$el);
50
        },
51
        show: function() {
52
            this.$el.addClass('mivhak-tab-pane-active');
53
            this.editor.focus();
54
            this.editor.gotoLine(0); // Needed in order to get focus
55
            
56
            // Recalculate scrollbar positions based on the now visible element
57
            this.vscroll.initialize();
58
            this.hscroll.initialize();
59
        },
60
        hide: function() {
61
            this.$el.removeClass('mivhak-tab-pane-active');
62
        },
63
        setEditor: function() {
64
            
65
            // Remove redundant space from code
66
            this.resource.pre.textContent = this.resource.pre.textContent.trim(); 
67
            
68
            // Set editor options
69
            this.editor = ace.edit(this.resource.pre);
70
            this.editor.setReadOnly(!this.mivhakInstance.options.editable);
71
            this.editor.setTheme("ace/theme/"+this.getTheme());
72
            this.editor.setShowPrintMargin(false);
73
            this.editor.renderer.setShowGutter(this.mivhakInstance.options.lineNumbers);
74
            this.editor.getSession().setMode("ace/mode/"+this.resource.lang);
75
            this.editor.getSession().setUseWorker(false); // Disable syntax checking
76
            this.editor.getSession().setUseWrapMode(true); // Set initial line wrapping
77
78
            this.editor.setOptions({
79
                maxLines: Infinity,
80
                firstLineNumber: this.resource.startLine,
81
                highlightActiveLine: false,
82
                fontSize: parseInt(14)
83
            });
84
            
85
            // Update source content for the live preview
86
            if(this.mivhakInstance.options.editable)
87
            {
88
                var $this = this;
89
                this.editor.getSession().on('change', function(a,b,c) {
90
                    $this.mivhakInstance.resources.update($this.index, $this.editor.getValue());
91
                });
92
            }
93
        },
94
        markLines: function()
95
        {
96
            if(!this.resource.mark) return;
97
            var ranges = strToRange(this.resource.mark),
98
                i = ranges.length,
99
                AceRange = ace.require("ace/range").Range;
100
101
            while(i--)
102
            {
103
                this.editor.session.addMarker(
104
                    new AceRange(ranges[i].start, 0, ranges[i].end, 1), // Define the range of the marker
105
                    "ace_active-line",     // Set the CSS class for the marker
106
                    "fullLine"             // Marker type
107
                );
108
            }
109
        }
110
    }
111
});